<%@ page import="javax.swing.text.StyledEditorKit.ForegroundAction"%>
<%@ page import="de.home.db.DBService"%>
<%@ page import="de.home.db.model.Floor"%>
<%@ page import="de.home.db.model.Room"%>
<%@ page import="de.home.db.model.RemotePowerPoint"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.util.*"%>
<!DOCTYPE>
<html>
<head>
<title>Verwaltung</title>
<link rel="stylesheet" type="text/css" href="css/generalStyle.css">
<link rel="stylesheet" type="text/css" href="css/styleAdmin.css">
<script src="js/jquery-2.1.1.js"></script>
<script src="js/generalScript.js" type="text/javascript"></script>
<script src="js/scriptAdmin.js" type="text/javascript"></script>
</head>
<body>
	<div id="title">
		<div>
			<a id="top" href="index.jsp"></a>
			<a id="normal"></a>
			<h1>Administrationsbereich</h1>
		</div>
	</div>
	
	<%
	   String error = (String) request.getAttribute("error");
	   if ( error == null ) {
	      error = "";
	   }

	   String msg = (String) request.getAttribute("msg");
	   if ( msg == null ) {
	      msg = "";
	   }
	   
	   String warn = (String) request.getAttribute("warning");
	   if ( warn == null ) {
	      warn = "";
	   }
	%>

	<div id="errorBox"><%=error%></div>
	<div id="infoBox"><%=msg%></div>
	<div id="warningBox"><%=warn%></div>

	<%
	   DBService dbService = DBService.getInstance();
	   List<Floor> floors = dbService.getFloorsWithoutRooms();
	   if ( floors.isEmpty() ) {
	%>
	<div id="warningBox">KEINE Eintr&auml;ge gefunden!</div>
	<div id="leftSide">
	<div id="addFloorSection">
		 <form action="DBServlet" method="POST" id="addFloor">
      <input type="hidden" name="action" value="createFloor"/>
      <input type="text" name="name" value="" placeholder="Name"/>
      <input type="submit" value="Stockwerk anlegen">
   </form>
	</div>
	</div>
	<%
	   } else {
	%>
		<div id="leftSide">
		<ul id="floorChoiceAdmin">
			<%
			   String selectedFloorStringId = request.getParameter("selectedFloorId");
			      boolean found = false;
			      Floor selectedFloor = null;
			      int id = -1;
			      if ( selectedFloorStringId != null ) {
			         id = Integer.parseInt(selectedFloorStringId);
			         for (int i = 0; i < floors.size(); i++) {
			            if ( floors.get(i).getId() == id ) {
			               found = true;
			            }
			         }
			      }

			      if ( !found ) {
			         selectedFloor = floors.get(0);
			%>
			<li class="floorSelect"
				data-floorId="<%=floors.get(0).getId()%>"><a id="activeFloor"><%=floors.get(0).getName()%></a></li>
			<%
			   for (int i = 1; i < floors.size(); i++) {
			%>
			<li class="floorSelect" data-floorId="<%=floors.get(i).getId()%>"><a><%=floors.get(i).getName()%></a></li>
			<%
			   }
			      } else {

			         for (int i = 0; i < floors.size(); i++) {
			            if ( floors.get(i).getId() == id ) {
			               selectedFloor = floors.get(i);
			%>
			<li class="floorSelect" 
				data-floorId="<%=floors.get(i).getId()%>"><a id="activeFloor"><%=floors.get(i).getName()%></a></li>
			<%
			   } else {
			%>
			<li class="floorSelect" data-floorId="<%=floors.get(i).getId()%>"><a><%=floors.get(i).getName()%></a></li>
			<%
			   }
			         }
			      }
			%>
		</ul>
		<div id="addFloorSection">
		 <form action="DBServlet" method="POST" id="addFloor">
      <input type="hidden" name="action" value="createFloor"/>
      <input type="text" name="name" value="" placeholder="Name"/>
      <input type="submit" value="Stockwerk anlegen">
   </form>
	</div>
	</div><!--

	--><div id="content">

		<form action="DBServlet" method="POST" id="floorAction">
			<input type="hidden" name="action" value="deleteFloor" /> <input
				type="hidden" name="id" value="<%=selectedFloor.getId()%>" /> <input
				type="submit" value="L&ouml;schen">
		</form>
		<h1 id="floorName"><%=selectedFloor.getName()%></h1>
		<%
		   List<Room> rooms = dbService.getRooms(selectedFloor.getId());
		      if ( rooms.isEmpty() ) {
		%>
		<h1 id="noEntry">KEINE Zimmer gefunden!</h1>
		<%
		   } else {
		%>
		<ul class="rooms">
			<%
			   for (Room r : rooms) {
			%>

			<li class="room">
				<form action="DBServlet" method="POST" class="roomAction">
					<input type="hidden" name="action" value="deleteRoom" /> <input
						type="hidden" name="id" value="<%=r.getId()%>" /> <input
						type="submit" value="L&ouml;schen">
				</form>
				<h1 class="roomTitle"><%=r.getName()%></h1>

				<table>

					<tr>
						<th>Ger&auml;te Name</th>
						<th>Haus Code</th>
						<th>Ger&auml;tenummer</th>
						<th>Hersteller</th>
						<th></th>
					</tr>

					<%
					   for (RemotePowerPoint rpp : r.getRemotePowerPoints()) {
					%>
					<tr data-fId="<%=rpp.getId()%>">
						<td style="display: none;" name="tableData"><%=rpp.getId()%></td>
						<td name="tableData"><%=rpp.getGeraetename()%></td>
						<td name="tableData"><%=rpp.getHauscode()%></td>
						<td name="tableData"><%=rpp.getGeraetenummer()%></td>
						<td name="tableData"><%=rpp.getHersteller()%></td>
						<td class="controlTd"><input type="submit"
							name="editRemoteButton" class="editRemote" value="&Auml;ndern">
							<%-- <form action="DBServlet" method="POST" name="editRemoteForm">
                           <input type="hidden" name="id" value="<%=rpp.getId()%>"/>
                           <input type="hidden" name="geraetename" value="<%=rpp.getGeraetename()%>"/>
                           <input type="hidden" name="hauscode" value="<%=rpp.getHauscode()%>"/>
                           <input type="hidden" name="geraetenummer" value="<%=rpp.getGeraetenummer()%>"/>
                           <input type="hidden" name="hersteller" value="<%=rpp.getHersteller()%>"/>
                        </form> --%>
							<form action="DBServlet" method="POST" class="deleteRemote">
								<input type="hidden" name="action" value="deleteRemote" /> <input
									type="hidden" name="rppId" value="<%=rpp.getId()%>" /> <input
									type="submit" value="L&ouml;schen">
							</form></td>
					</tr>
					<%
					   }
					%>

				</table>

				<form action="DBServlet" method="POST" class="addRemote">
					<input type="hidden" name="action" value="createRemote" /> <input
						type="hidden" name="stockwerk_id"
						value="<%=selectedFloor.getId()%>" /> <input type="hidden"
						name="zimmer_id" value="<%=r.getId()%>" />

					<table>
						<tr>
							<td><input class="remoteInput" type="text"
								name="geraetename" value="" placeholder="Ger&auml;tename" /></td>
							<td><input class="remoteInput" type="text" name="hauscode"
								value="" placeholder="Hauscode" /></td>
							<td><input class="remoteInput" type="text"
								name="geraetenummer" value="" placeholder="Ger&auml;te Nummer" /></td>
							<td><input class="remoteInput" type="text" name="hersteller"
								value="" placeholder="Hersteller" /></td>
							<td><input class="remoteInput" type="submit"
								value="Funksteckdose anlegen"></td>
						</tr>
					</table>

				</form>

			</li>
			<%
			   }
			%>
		</ul>
		<%
		   }
		%>
		<form action="DBServlet" method="POST" id="addRoom">
			<input type="hidden" name="action" value="createRoom" /> <input
				type="hidden" name="stockwerk_id" value="<%=selectedFloor.getId()%>" />
			<input type="text" name="name" value="" placeholder="Name" /> <input
				type="submit" value="Zimmer anlegen">
		</form>

	</div>
	<%
	   }
	%>

</body>
</html>